package engine.data
{
	import engine.net.datatype.ByteArrayEx;
	
	import flash.events.EventDispatcher;

	public class Record extends EventDispatcher
	{
		/**服务器表格名*/
		private var _recName	:String	=	"";
		
		/**表格二维数组*/
		private var _recArr	:Array	=	new Array();
		
		/**用于记录表格每列的数据类型*/
		private var _nColType	:Array	=	new Array();
		
		/**
		 * 创建表格数据
		 * @param val	表格名
		 * 
		 */		
		public function Record(val:String)
		{
			recName = val;
		}

		/**表格二维数组*/
		public function get recArr():Array
		{
			return _recArr.concat();
		}

		/**
		 * @private
		 */
		public function set recArr(value:Array):void
		{
			_recArr = value;
		}

		/**服务器表格名*/
		public function get recName():String
		{
			return _recName;
		}

		/**
		 * @private
		 */
		public function set recName(value:String):void
		{
			_recName = value;
		}
		
		/**
		 * 设置表格结构 
		 * @param nColType	：记录表格每列的信息
		 * 
		 */		
		public function SetColType(nColType:Array):void
		{
			_nColType = nColType;
		}
		
		/**
		 * 取表格列数 
		 * @return 
		 * 
		 */		
		public function GetCols():int
		{
			return _nColType.length;
		}
		
		/**
		 * 插入数据
		 * @param row	：插入到row行
		 * @param count	：插入count条数据
		 * @param msg	：数据
		 * 
		 */		
		public function AddRow(rowIndex:int,count:int,msg:ByteArrayEx):void
		{
			//表格列数
			var tColLength	:int	=	GetCols();
			
			//表格列数据类型
			var tColType	:uint;
			
			//待插入的数组
			var tDataArr	:Array	=	new Array();
			
			//待插入的一行数据
			var tColDataArr	:Array;
			
			for(var i:int = 0 ; i < count ; i ++)
			{
				tColDataArr	=	new Array();
				for(var j:int = 0 ; j < tColLength ; j++)
				{
					tColType = _nColType[j];
					tColDataArr[j] = msg.GetDataByType(tColType);
				}
				tDataArr.push(tColDataArr);
			}
			
//			//将数据插入到本地数据缓存中
//			_recArr.splice(rowIndex,0,tDataArr);
			
			var tIndex:int;
			//派发绑定
			for(var k:int = 0 ; k < count ; k ++)
			{
				tIndex	=	rowIndex+k;
				
				//将数据插入到本地数据缓存中
				_recArr.splice(tIndex,0,tDataArr[k]);
				
				BindManage.getInstance().CallRecord(this,tIndex,_recArr[tIndex],true);
			}
		}
		
		/**
		 * 修改表格单元格， 
		 * @param nCount
		 * @param msg	（包含行列及修改值）
		 * 
		 */		
		public function ChangeRecordGrid(nCount:uint,msg:ByteArrayEx):void
		{
			//表格列数据类型
			var tColType	:uint;
			
			for(var i:int = 0 ; i < nCount; i++)
			{
				var		nRow:uint	=	msg.readUnsignedShort();
				var		nCol:uint	=	msg.readByte();
				
				if(_recArr[nRow] == null)
				{
					_recArr[nRow]	=	new Array();
				}
				
				tColType = _nColType[nCol];
				
				_recArr[nRow][nCol]	=	msg.GetDataByType(tColType);
				
				//派发绑定
				BindManage.getInstance().CallRecord(this,nRow,_recArr[nRow],true);
			}	
		}
		
		/**
		 * 删除表格某一列 
		 * @param rowIndex
		 * 
		 */		
		public function DelRow(rowIndex:uint):void
		{
			//先删除行
			BindManage.getInstance().CallRecord(this,rowIndex,_recArr.splice(rowIndex,1)[0],false);
		}
		
		/**
		 * 清空表格
		 */		
		public function RecordClear():void
		{
			var tSize:int	=	_recArr.length;
			var tempArray:Array	=	_recArr;
			_recArr	=	new Array();
			//先删除行
			for(var j:int = 0 ; j < tSize; j ++)
			{
				//DelRow(j);
				BindManage.getInstance().CallRecord(this,j,tempArray[j],false);
			}

		}		
	}
}